<template>
	<view class="animal_index">
		<view class="animal_header">
			<view class="animal_header_title">
				<view class="th2">Adopt a
				</view>
				<view class="th1">Friend</view>
			</view>
			<image aspectFit class="index_header_img" src="/static/index-header.png" mode=""></image>
		</view>
		<view class="animal_search" @click="searchGo">
			<image class="search_icon" src="/static/search.png" mode=""></image>
			<text class="sPlo">请输入需要搜索的内容...</text>
		</view>
		<view class="animal_main">
			<view class="hot_kind">
				<view class="hot_kind_list">
					<view class="hot_item" @click="changeAct(index)" v-for="(item,index) in kindList">
						<view class="hot_item_img" :class="active==index? 'active':''">
							<image :src="active==index?item.selPath:item.path" mode=""></image>
							<view class="line" :class="active==index? 'active':''"></view>
						</view>
						<text class="hot_item_text">{{item.name}}</text>
					</view>


				</view>

			</view>
			<view class="animal_list">
				<view class="animal_list_title">hot animal</view>
				<view class="animal_list_main">

					<!-- v-for="(item,index) in 4" -->

					<view class="animal_list_tab" v-for="(item,index) in hotList" :key="item.id">
						<view class="tab_img" @click="jumpGo(item.id)">
							<image :src="item.pet_coverURL" mode="aspectFill"></image>
						</view>
						<view class="tab_desc">
							<view class="tdesc_tit">
								{{item.pet_engName||"暂无数据"}}
							</view>
							<view class="tdesc_bot">
								{{item.pet_name||"暂无数据"}}
							</view>
							<view class="tdesc_bot">
								{{item.pet_life||"暂无数据"}}
							</view>
							<view class="tab_collect" :class="item.flat==true? 'active':''"
								@click="changeCol(index,item['flat'])">
								<image src="/static/collect.png" mode=""></image>
							</view>
						</view>
					</view>


				</view>
			</view>
		</view>
		<view-tabbar :idx="'0'"></view-tabbar>
	</view>
</template>

<script>
	import Tabbar from '@/components/tabbar.vue'
	import {
		selData,
		addCol,
		judgmentUserCol
	} from "@/api/index.js"
	export default {
		data() {
			return {
				active: 0,
				current: 0,
				hotList: [],
				kindList: [{
					name: "猫",
					path: "/static/cat.png",
					selPath: '/static/cat-o.png'
				}, {
					name: "狗",
					path: "/static/dog.png",
					selPath: '/static/dog-o.png'
				}, {
					name: "爬行",
					path: "/static/rabbit.png",
					selPath: '/static/rabbit-o.png'
				}, {
					name: "小宠物",
					path: "/static/bird.png",
					selPath: '/static/bird-o.png'
				}]
			}
		},
		components: {
			'view-tabbar': Tabbar,
		},
		onLoad() {

		},

		onShow() {
			// if(uni.getStorageSync("uid")){

			// }else{
			// 	this.getList()
			// }
			this.getList()
			uni.hideTabBar({
				animation: false
			})
		},
		methods: {
			async selCol(uid, arr) {
				let newArr = Promise.all(
					arr.map((item, index) => {

						return (async () => {
							await judgmentUserCol(uid, item.id).then((content) => {
								arr[index].flat = content.data.flat
							})
							return item
						})()
					})
				).then((res) => {
					console.log(res)
					this.hotList = res
					uni.hideLoading()
				})
				console.log(newArr, 456456456)
			},
			async getList() {
				uni.showLoading({
					title: "加载中"
				})
				await selData(this.active).then((data) => {
					if (uni.getStorageSync("uid")) {
						this.selCol(uni.getStorageSync("uid"), data.data.rows)
					} else {
						console.log(789)
						this.hotList = data.data.rows
						this.hotList.map((item) => {
							item['flat'] = false
						})
						uni.hideLoading()
					}

				})
			},
			changeAct(idx) {
				this.active = idx;
				this.getList()
			},
			jumpGo(id) {
				uni.setStorageSync("id", id)
				uni.navigateTo({
					url: `/pages/detail/detail`
				})
			},
			searchGo() {
				uni.navigateTo({
					url: "/pages/search/search"
				})
			},
			changeCol(idx, flat) {
				if (uni.getStorageSync("uid")) {
					this.hotList[idx].flat = !flat
					console.log(this.hotList[idx].flat)
					let uid = uni.getStorageSync("uid");
					let did = this.hotList[idx].id
					addCol(uid, did).then((content) => {
						console.log(content.data.msg)
					})
				} else {
					uni.showToast({
						title: "收藏失败,请先登录!",
						icon: "none"
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.animal_index {
		height: 100%;
		background-color: #a1b275;

		.animal_search {
			margin: 0 15px;
			height: 50px;
			background-color: white;
			border-radius: 16px;
			display: flex;
			align-items: center;
			background-color: #ffffff;

			.search_icon {
				width: 20px;
				height: 20px;
				margin: 10px;
			}

			.sPlo {
				font-size: 14px;
				color: #BBC3CE;
				font-weight: 500;
			}

		}

	}

	.animal_header {
		width: 100%;
		height: 150px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.index_header_img {
		display: block;
		width: 172px;
		height: 130px;
		margin-right: 20px;
		margin-top: 10px;
	}

	.animal_header_title {
		margin-top: 20px;
		margin-left: 40px;
		color: white;
		line-height: 36px;

		.th2 {
			font-size: 28px;
			font-weight: 100;
		}

		.th1 {
			font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
			font-size: 28px
		}
	}

	.animal_main {
		margin-top: 20px;
		border-radius: 32px 32px 0 0;
		background-color: #f3f2f2;
		padding-bottom: 70px;

		.hot_kind {
			height: 100px;
			padding: 0 20px;
			padding-top: 20px;
			overflow: scroll;
			display: flex;
			align-items: center;

			.hot_kind_list {
				display: flex;
			}

			.hot_item {
				width: 69px;
				margin-right: 20px;
				color: #a1b275;

				.hot_item_img {
					background-color: white;
					display: flex;
					width: 69px;
					justify-content: center;
					height: 60px;
					align-items: center;
					border-radius: 22px;
					position: relative;
					transition: .3s;

					&.active {
						background-color: #a1b275;
						box-shadow: 0 3px 10px #a1b275;
					}

					.line {
						height: 3px;
						width: 19px;
						position: absolute;
						background-color: transparent;
						transition: .3s;
						bottom: -8px;
						left: 50%;
						transform: translateX(-50%);

						&.active {
							background-color: #a1b275;
						}
					}

					image {
						width: 32px;
						height: 32px;
					}


				}

				.hot_item_text {
					padding-top: 10px;
					font-size: 13px;
					display: block;
					text-align: center;
				}
			}
		}
	}

	.animal_list {
		margin: 0 20px;
		margin-top: 20px;
		font-size: 20px;

		.animal_list_title {
			color: #5c6542;
		}

		.animal_list_main {
			padding-bottom: 20px;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.animal_list_tab {
				width: 162px;
				height: 229px;
				border-radius: 28px;
				background-color: white;
				margin-top: 20px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-between;

				&:nth-child(even) {
					transform: translateY(20px);
				}

				.tab_img {
					width: 152px;
					height: 152px;
					margin-top: 5px;
					border-radius: 26px;
					overflow: hidden;

					image {
						height: 100%;
						width: 100%;
						object-fit: cover;
					}

				}

				.tab_desc {
					margin-left: 4px;
					height: 60px;
					width: 142px;
					border-radius: 26px;
					margin-bottom: 5px;
					position: relative;

					.tab_collect {
						position: absolute;
						width: 21px;
						height: 21px;
						background-color: #ECEBEB;
						bottom: 10px;
						right: 4px;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						transition: .3s;

						image {
							height: 12px;
							width: 12px;
						}

						&.active {
							background-color: #ffd967;
							box-shadow: 0 2px 6px #ffd967;
						}
					}

					.tdesc_tit {
						margin-top: 2px;
						font-size: 14px;
						color: #333;
						text-overflow: ellipsis;
						overflow: hidden;
						white-space: nowrap;
					}

					.tdesc_bot {
						margin-top: 4px;
						font-size: 12px;
						color: gray;

						&:last-child {
							margin-top: 0px;
						}
					}
				}
			}

		}

	}
</style>